package boni.si;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import boni.baza.AABDatabaseManager;
import boni.infoRazredi.LokalSimple;
import boni.infoRazredi.LokalZaMapo;
import boni.infoRazredi.RazdaljaMedLokali;
import boni.si.*;
import boni.si.R.drawable;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MojLocationListner implements LocationListener{

	Context context;
	MapController mc;
	MapView mapView;
	List<Overlay> listOfOverlays;
	Geocoder geoCoder;
	int kakoDalecZelimIti;
	AABDatabaseManager arhiver;
	Location enaLokacija;
	
	TextView tvOddaljenostText, tvRazdalja;
	
	public MojLocationListner(Context context, MapController _mc, MapView _mapView, List<Overlay> _listOfOverlays, int kakoDalec,String latitude, String longitude, TextView tvRazdalja, TextView tvOddaljenost) {
		super();
		this.context=context;
		mc=_mc;
		this.tvOddaljenostText = tvOddaljenost;
		this.tvRazdalja = tvRazdalja;
        kakoDalecZelimIti = kakoDalec;
        if(latitude != null){
        	enaLokacija = new Location("");
        	this.enaLokacija.setLatitude(Double.parseDouble(latitude));
    		this.enaLokacija.setLongitude(Double.parseDouble(longitude));
        }
		mapView=_mapView;
		listOfOverlays=_listOfOverlays;
		
		listOfOverlays = mapView.getOverlays();
        //listOfOverlays.clear();
        
        geoCoder = new Geocoder(context);
        
        arhiver = new AABDatabaseManager(context);
		//mc.setZoom(15);
	}
	
	
	@Override
	public void onLocationChanged(Location loc)
	{
		System.out.println("LOCATION CHANGE");
		loc.getLatitude();
		loc.getLongitude();
		
		GeoPoint point = new GeoPoint((int) (loc.getLatitude() * 1E6),(int) (loc.getLongitude() * 1E6));

		MojItemizedOverlay itemizedoverlayJaz = new MojItemizedOverlay(context.getResources().getDrawable(R.drawable.pushpin_mylocation), context);
        MojItemizedOverlay itemizedoverlayBlizu = new MojItemizedOverlay(context.getResources().getDrawable(R.drawable.pushpin_blizu), context);
        System.out.println("Tocka: " + point.getLatitudeE6() + ", " + point.getLongitudeE6());
        OverlayItem overlayitem;
        
		//listOfOverlays.clear();
		overlayitem = new OverlayItem(point, "Moja trenutna lokacija", "");
		itemizedoverlayJaz.addOverlay(overlayitem);
        
		if(enaLokacija != null)
		{
			float razdalja = loc.distanceTo(enaLokacija);
			if(razdalja > 1000.0)
			{
				DecimalFormat twoDForm = new DecimalFormat("#.#");
				tvOddaljenostText.setText("Oddaljenost(km): ");
				tvRazdalja.setText(""+(Double.valueOf(twoDForm.format(razdalja/1000))));
			}
			else
			{
				int razdaljaInt = Math.round(razdalja);
				tvOddaljenostText.setText("Oddaljenost(m): ");
				tvRazdalja.setText(""+razdaljaInt);
			}
			overlayitem = new OverlayItem(point, "Moja trenutna lokacija", "");
			itemizedoverlayJaz.addOverlay(overlayitem);
			if(listOfOverlays.size() == 2)
				listOfOverlays.remove(listOfOverlays.size()-1);
			if(itemizedoverlayJaz.size() > 0)
				listOfOverlays.add(itemizedoverlayJaz);
			mapView.invalidate();
			
		}
		else
		{
			listOfOverlays = mapView.getOverlays();
			listOfOverlays.clear();
			
	        ArrayList<LokalZaMapo> vsiLokali = arhiver.dobiVseLokaleZaMapo();
	        
	        GeoPoint mojaZadnjaGeoTocka = null;

	        try
	        {
				try
	        	{
					mojaZadnjaGeoTocka = new GeoPoint((int) (loc.getLatitude() * 1E6), (int)(loc.getLongitude()* 1E6));
				}catch (Exception e) {}
				if(mojaZadnjaGeoTocka != null)
				{
					mc.animateTo(mojaZadnjaGeoTocka);
					
					overlayitem = new OverlayItem(mojaZadnjaGeoTocka, "Moja trenutna lokacija", "");
					itemizedoverlayJaz.addOverlay(overlayitem);
					
					Location restLoc = new Location("");
					float distance;
					GeoPoint p;
					
					for(int i=0; i<vsiLokali.size();i++)
					{
						restLoc.setLongitude(Double.parseDouble(vsiLokali.get(i).getLongitude()));
						restLoc.setLatitude(Double.parseDouble(vsiLokali.get(i).getLangitude()));
						
						distance = restLoc.distanceTo(loc);
						int distInt = (int)distance;
						if(distInt < 25000)
						{
							p = new GeoPoint((int) (Float.parseFloat(vsiLokali.get(i).getLangitude()) * 1E6), 
											 (int) (Float.parseFloat(vsiLokali.get(i).getLongitude()) * 1E6));
							overlayitem = new OverlayItem(p, vsiLokali.get(i).getIme() +"\n" +
															vsiLokali.get(i).getNaslov() +"\n" + 
															vsiLokali.get(i).getMesto(), "");
							itemizedoverlayBlizu = new MojItemizedOverlay(context.getResources().getDrawable(R.drawable.pushpin_blizu), context);
							itemizedoverlayBlizu.addOverlay(overlayitem);
							listOfOverlays.add(itemizedoverlayBlizu);
						}
					}
					overlayitem = new OverlayItem(mojaZadnjaGeoTocka, "Moja trenutna lokacija", "");
					itemizedoverlayJaz.addOverlay(overlayitem);
		
					/*
					if(itemizedoverlayBlizu.size() > 0)
						listOfOverlays.add(itemizedoverlayBlizu);
					*/
					if(mojaZadnjaGeoTocka != null)
					{
						listOfOverlays.add(itemizedoverlayJaz);
						mc.setZoom(15);
					}
					else
					{
						mc.setZoom(8);
					}
					mapView.invalidate();
				}
				else
				{ System.out.println("Moja zadnja tocka = null"); }
			} catch (Exception e)
			{ System.out.println("IZJEMA X"); e.printStackTrace(); }
		}
	}
	
	private boolean preveriRadij(LokalSimple lokalx, ArrayList<LokalSimple>dodaniLokali)
	{
		Location loc1 = new Location("s");
		loc1.setLatitude(Double.parseDouble(lokalx.getLangitude()));
		loc1.setLongitude(Double.parseDouble(lokalx.getLongitude()));
		
		for(int i=0; i<dodaniLokali.size(); i++)
		{
			Location loc2 = new Location("a");
			loc2.setLongitude(Double.parseDouble(dodaniLokali.get(i).getLongitude()));
			loc2.setLatitude(Double.parseDouble(dodaniLokali.get(i).getLangitude()));
			
			float distance = loc2.distanceTo(loc1);
			int distn = (int) distance;
			if(distn < 500)
				return false;
		}
		return true;
	}
	
	@Override
	public void onProviderDisabled(String provider) {
		//Toast.makeText( context,"Gps Disabled",Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderEnabled(String provider) {
		//Toast.makeText( context,"Gps Enabled",Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		//Toast.makeText( context,"Gps status changed",Toast.LENGTH_SHORT).show();
		
	}
}
